package com.yubest.javaagent;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.IOException;

/**
 * 使用 com.yubest.javaagent.MainAgent，监控 com.yubest.javaagent.TargetApp 的执行
 *
 * @Author: hweiyu
 * @Date: 2022/12/13 11:04
 */
public class Monitor {

    public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
        VirtualMachine vm = null;
        try {
            // 通过 jps 获取目标 java 进程的 pid
            String pid = "4460";
            vm = VirtualMachine.attach(pid);
            vm.loadAgent("E:/workspace/blog/tech-stack-easy/java-agent/target/java-agent-1.0-SNAPSHOT.jar");
        } finally {
            if (null != vm) {
                vm.detach();
            }
        }
    }
}
